package com.example.waterdispenserm2.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.waterdispenserm2.R;

/**
 * 错误信息列表适配器
 */
public class ErrorAdapter extends BaseAdapter {
    private final Context context;
    private final byte[] errors;

    public ErrorAdapter(Context context, byte[] errors) {
        this.context = context;
        this.errors = errors;
    }

    @Override
    public int getCount() {
        return errors.length;
    }

    @Override
    public Object getItem(int position) {
        return errors[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.list_item_error, parent, false);
        }
        
        TextView errorCode = convertView.findViewById(R.id.tv_error_code);
        TextView errorDesc = convertView.findViewById(R.id.tv_error_desc);
        
        byte code = errors[position];
        errorCode.setText(String.format("错误码: %02X", code));
        errorDesc.setText(getErrorDescription(code));
        
        return convertView;
    }

    /**
     * 获取错误描述
     * @param code 错误码
     * @return 错误描述文本
     */
    private String getErrorDescription(byte code) {
        switch (code) {
            case 0x00: return "无错误";
            case 0x01: return "缺水: 水泵电流异常";
            case 0x02: return "出水NTC故障";
            case 0x04: return "抽水泵故障";
            case 0x05: return "加热开路故障";
            case 0x06: return "结冰报警";
            case 0x07: return "高温报警";
            default: return "未知错误";
        }
    }
}